/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.inventory;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Stack;
import static rpg.util.yaml.tokenizer.TokenType.*;
import rpg.util.yaml.tokenizer.*;

/**
 *
 * @author rothens
 */
public class ItemParser {

    public ItemParser() {
        Stack<Token> parsedTokens = new Stack<Token>();
        Stack<String> keys = new Stack<String>();
        HashMap<String, Object> itemStats = new HashMap<String, Object>();
        HashSet<String> enchants = new HashSet<String>();
        try {
            Iterator<Token> it = new Tokenizer(new FileReader("test/test.yml")).iterator();
            while (it.hasNext()) {
                Token t = it.next();
                if (t.type == BLOCK_ENTRY) {
                    while (t.type != BLOCK_MAPPING_START && it.hasNext()) {
                        t = it.next();
                    }
                    parsedTokens.push(t);
                    while (!parsedTokens.isEmpty() && it.hasNext()) {
                        t = it.next();
                        if (t.type == SCALAR) {
                            Token pop = parsedTokens.pop();
                            if (pop.type == KEY) {
                                keys.push(((ScalarToken) t).getValue());
                            } else if (pop.type == VALUE) {
                                itemStats.put(keys.pop(), ((ScalarToken) t).getValue());
                            } else if (pop.type == BLOCK_ENTRY) {
                                if (keys.peek().equalsIgnoreCase("enchants")) {
                                    enchants.add(((ScalarToken) t).getValue());
                                }
                            }
                        }
                        if (t.type != SCALAR && t.type != BLOCK_END) {
                        parsedTokens.push(t);
                        }
                        if (t.type == BLOCK_END) {
                            Token be = parsedTokens.pop();
                            if (be.type == BLOCK_SEQUENCE_START) {
                                itemStats.put(keys.pop(), enchants.clone());
                                enchants.clear();
                                parsedTokens.pop();
                            }
                        }
                    }
                    System.out.println("Item:");
                    for (String s : itemStats.keySet()) {
                        System.out.println(s + ": " + itemStats.get(s));
                    }
                    itemStats.clear();
                    System.out.println();
                }

            }
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());
        }
    }
}
